package coroutines

/**
 * async 的结构化并行
 */
import kotlinx.coroutines.*
import kotlin.system.measureTimeMillis

private suspend fun do1():Int{
    println("do1 in ${getThread()}")
    delay(1000)
    return 1
}
private suspend fun do2():Int{
    println("do2 in ${getThread()}")
    delay(1000 *2)
    return 1
}

//fun main()= runBlocking {
//   val times = measureTimeMillis {
//        val d1 =GlobalScope.async { do1() }
//        val d2 = GlobalScope.async { do2() }
//        val r = d1.await()+d2.await()
//        println("result:$r")
//    }
//    println("main end in thread ${Thread.currentThread().name},times:$times")
//}
suspend fun main() {
   val times = measureTimeMillis {
       coroutineScope {
           val d1 =async { do1() }
           val d2 = async { do2() }
           val r = d1.await()+d2.await()
           println("result:$r")
       }
    }
    println("main end in thread ${Thread.currentThread().name},times:$times")
}